//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "MonteCarloCDOEngine1.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Experimental/Credit/RandomDefaultModel.h>
#include <gen/QL/Experimental/Credit/MidPointCDOEngine.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine1::CMonteCarloCDOEngine1 (Cephei::QL::Experimental::Credit::IRandomDefaultModel^ rdm, UInt64 samples) : CMidPointCDOEngine(CMonteCarloCDOEngine1::typeid)
{
    CRandomDefaultModel^ _Crdm;
    try
    {
#ifdef HANDLE
        _phMonteCarloCDOEngine1 = NULL;
#endif
        _Crdm = safe_cast<CRandomDefaultModel^> (rdm);
        _Crdm->Lock();
        boost::shared_ptr<QuantLib::RandomDefaultModel> _rdm = static_cast<boost::shared_ptr<QuantLib::RandomDefaultModel>> (_Crdm->GetShared ()); 
        QuantLib::Size _samples = (QuantLib::Size)ValueHelper::Convert (samples); //d
        _ppMonteCarloCDOEngine1 = new boost::shared_ptr<QuantLib::MonteCarloCDOEngine1> (new QuantLib::MonteCarloCDOEngine1 ( _rdm,  _samples ));
        SetMidPointCDOEngine (boost::dynamic_pointer_cast<QuantLib::MidPointCDOEngine> (*_ppMonteCarloCDOEngine1));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Crdm != nullptr) _Crdm->Unlock();
    }
}
Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine1::CMonteCarloCDOEngine1 (boost::shared_ptr<QuantLib::MonteCarloCDOEngine1>& childNative, Object^ owner) : CMidPointCDOEngine(CMonteCarloCDOEngine1::typeid)
{
#ifdef HANDLE
	_phMonteCarloCDOEngine1 = NULL;
#endif
	_ppMonteCarloCDOEngine1 = &childNative;
    _ppMidPointCDOEngine = new boost::shared_ptr<QuantLib::MidPointCDOEngine> (boost::dynamic_pointer_cast<QuantLib::MidPointCDOEngine> (*_ppMonteCarloCDOEngine1));
}
Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine1::CMonteCarloCDOEngine1 (QuantLib::MonteCarloCDOEngine1& childNative, Object^ owner) : CMidPointCDOEngine(CMonteCarloCDOEngine1::typeid)
{
#ifdef HANDLE
	_phMonteCarloCDOEngine1 = NULL;
#endif
	_ppMonteCarloCDOEngine1 = new boost::shared_ptr<QuantLib::MonteCarloCDOEngine1> (&childNative);
    _ppMidPointCDOEngine = new boost::shared_ptr<QuantLib::MidPointCDOEngine> (boost::dynamic_pointer_cast<QuantLib::MidPointCDOEngine> (*_ppMonteCarloCDOEngine1));
    _MonteCarloCDOEngine1Owner = owner;
    _MidPointCDOEngineOwner = owner;
}

Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine1::CMonteCarloCDOEngine1 (CMonteCarloCDOEngine1^ copy) : CMidPointCDOEngine(CMonteCarloCDOEngine1::typeid)
{
#ifdef HANDLE
	_phMonteCarloCDOEngine1 = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMonteCarloCDOEngine1 = new boost::shared_ptr<QuantLib::MonteCarloCDOEngine1> (copy->GetShared());
        _ppMidPointCDOEngine = new boost::shared_ptr<QuantLib::MidPointCDOEngine> (boost::dynamic_pointer_cast<QuantLib::MidPointCDOEngine> (*_ppMonteCarloCDOEngine1));
    }
}
Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine1::CMonteCarloCDOEngine1 (PLATFORM::Type^ t) : CMidPointCDOEngine(CMonteCarloCDOEngine1::typeid)
{
#ifdef HANDLE
	_phMonteCarloCDOEngine1 = NULL;
#endif
	if (!t->IsSubclassOf(CMonteCarloCDOEngine1::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine1::CMonteCarloCDOEngine1 (QuantLib::Handle<QuantLib::MonteCarloCDOEngine1>& childNative, Object^ owner)  : CMidPointCDOEngine(CMonteCarloCDOEngine1::typeid)
{
	_phMonteCarloCDOEngine1 = &childNative;
	_ppMonteCarloCDOEngine1 = &static_cast<boost::shared_ptr<QuantLib::MonteCarloCDOEngine1>>(childNative.currentLink());
    _ppMidPointCDOEngine = new boost::shared_ptr<QuantLib::MidPointCDOEngine> (boost::dynamic_pointer_cast<QuantLib::MidPointCDOEngine> (*_ppMonteCarloCDOEngine1));
    _MonteCarloCDOEngine1Owner = owner;
}
Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine1::CMonteCarloCDOEngine1 (QuantLib::Handle<QuantLib::MonteCarloCDOEngine1> childNative)  : CMidPointCDOEngine(CMonteCarloCDOEngine1::typeid)
{
	_phMonteCarloCDOEngine1 = &childNative;
	_ppMonteCarloCDOEngine1 = &static_cast<boost::shared_ptr<QuantLib::MonteCarloCDOEngine1>>(childNative.currentLink());
    _ppMidPointCDOEngine = new boost::shared_ptr<QuantLib::MidPointCDOEngine> (boost::dynamic_pointer_cast<QuantLib::MidPointCDOEngine> (*_ppMonteCarloCDOEngine1));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine1::CMonteCarloCDOEngine1 (QuantLib::MonteCarloCDOEngine1 childNative)  : CMidPointCDOEngine(CMonteCarloCDOEngine1::typeid)
{
#ifdef HANDLE
	_phMonteCarloCDOEngine1 = NULL;
#endif
	_ppMonteCarloCDOEngine1 = new boost::shared_ptr<QuantLib::MonteCarloCDOEngine1> (new QuantLib::MonteCarloCDOEngine1 (childNative));
    _ppMidPointCDOEngine = new boost::shared_ptr<QuantLib::MidPointCDOEngine> (boost::dynamic_pointer_cast<QuantLib::MidPointCDOEngine> (*_ppMonteCarloCDOEngine1));
}
#endif

Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine1::~CMonteCarloCDOEngine1 ()
{
    if (_ppMonteCarloCDOEngine1 != NULL)
    {
	    delete _ppMonteCarloCDOEngine1;
        _ppMonteCarloCDOEngine1 = NULL;
    }
}
Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine1::!CMonteCarloCDOEngine1 ()
{
    if (_ppMonteCarloCDOEngine1 != NULL)
    {
	    delete _ppMonteCarloCDOEngine1;
    }
}
QuantLib::MonteCarloCDOEngine1& Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine1::GetReference ()
{
    if (_ppMonteCarloCDOEngine1 == NULL) throw REFNEW NativeNullException ();
	return **_ppMonteCarloCDOEngine1;
}
boost::shared_ptr<QuantLib::MonteCarloCDOEngine1>& Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine1::GetShared ()
{
    if (_ppMonteCarloCDOEngine1 == NULL) throw REFNEW NativeNullException ();
	return *_ppMonteCarloCDOEngine1;
}
QuantLib::MonteCarloCDOEngine1* Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine1::GetPointer ()
{
    if (_ppMonteCarloCDOEngine1 == NULL) throw REFNEW NativeNullException ();
	return &**_ppMonteCarloCDOEngine1;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MonteCarloCDOEngine1>& Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine1::GetHandle ()
{
	if (_phMonteCarloCDOEngine1 == NULL)
	{
		_phMonteCarloCDOEngine1 = new Handle<QuantLib::MonteCarloCDOEngine1> (*_ppMonteCarloCDOEngine1);
	}
	return *_phMonteCarloCDOEngine1;
}
#endif
bool Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine1::HasNative () 
{
	return (_ppMonteCarloCDOEngine1 != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Credit::IMonteCarloCDOEngine1^ Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine1_Factory::Create (Cephei::QL::Experimental::Credit::IRandomDefaultModel^ rdm, UInt64 samples)
{
    return REFNEW CMonteCarloCDOEngine1 ( rdm,  samples);
}
